package redora.configuration.rdo.businessrules;

import org.apache.commons.codec.digest.DigestUtils;
import org.jetbrains.annotations.NotNull;
import redora.configuration.rdo.businessrules.base.RedoraTrashBusinessRulesBase;
import redora.configuration.rdo.model.RedoraTrash;
import redora.exceptions.BusinessRuleViolationException;
import redora.service.BusinessRuleViolation;

import java.util.Date;
import java.util.Set;

public class RedoraTrashBusinessRules extends RedoraTrashBusinessRulesBase {

    /**
     * Set the undoHash when it is empty
     */
    @NotNull
    public static Set<BusinessRuleViolation> onInsert(@NotNull RedoraTrash pojo) throws BusinessRuleViolationException {
        //Set undoHash before checking the business rules
        if (pojo.getUndoHash() == null) {
            //StringBuilder is about 25% faster in JDK1.6
            pojo.setUndoHash(DigestUtils.shaHex(
                    new StringBuilder(pojo.getObjectName()).append(pojo.getObjectId()).append(new Date()).toString()));
        }

        return RedoraTrashBusinessRulesBase.onInsert(pojo);
    }
}
